
var validBalloon="balloon2";


function IsNumeric(sText) {

	var ValidChars = "0123456789.";

	var char;

	var decimals = 0;



	for (i = 0, l=sText.length; i < l; i++) {

		char = sText.charAt(i);

		if (ValidChars.indexOf(char) == -1) {

			return false;

		}

		if ( char == '.' ) {

			decimals++;

		}

	}

	if ( decimals > 1 ) {

		return false;

	}

	return true;

}



// Function to calculate Periodic Payments.

function pmt() {

	// baloon value

	var fv = document.getElementById("balloon").value;

	// payment term

	var nper = document.getElementById("term").value;

	// interest rate

	var rate = document.getElementById("rate").value;

	// amount to be financed

	var pv = document.getElementById("amount").value;

	// baloon percent

	var balper = document.getElementById("balloon2").value;

	// baloon $

	var balpay = document.getElementById("balloon").value;



	// remove $ and , from the financed amount string, we want a float only

	pv = pv.replace(/[$]/g,"").replace(/,/g,"");

	// make sure the amount financed is not negative.

	if(pv != "" && IsNumeric(pv)) {

		if (pv < 0) {

			alert("The value of the amount to be financed (boat price) must not be less than $0");

			document.calculator.amount.focus();

			return false;

                 }

	} else {

		alert("The value of the amount financed (boat price) must be a valid number");

		document.calculator.amount.focus();

		return false;

	}

	document.getElementById("amount").value = pv;

	//used by program

	pv = -pv;

   

	//make sure the balper field is only b/w 0 and 100 and no negatives.

	if(balper != "" && IsNumeric(balper)) {

		if ((balper < 0) || (balper > 100)) {

			alert("The value for the balloon percentage must be between 0 and 100");

			return false;

		} else {

			document.getElementById("balloon").value = (((-(pv*(balper/100))*100))/100).toFixed(2);

		}

	} else {

		alert("The value for the balloon percentage must be a valid number %");

	}



	//make sure that all money is positive not negative	 

	if (fv < 0) {

		alert("The value for the balloon payment must not be less than $0");

		return false;

	}



	if ((fv == 0) || (fv == "") || (!IsNumeric(fv)))  {

		fv = -(pv*balper/100);

	}

	fv = parseFloat(fv);

	nper = parseFloat(nper);

	pv = parseFloat(pv);

	rate = parseFloat(rate);



	rate = eval((rate)/(12 * 100));

	if ( rate == 0 ) {

		// interest rate is 0

		pmt_value = - (fv + pv)/nper;

	} else {

		x = Math.pow(1 + rate,nper);

		pmt_value = -((rate * (fv + x * pv))/(-1 + x));

	}

	pmt_value = conv_number(pmt_value,2);		

	document.getElementById("monthly").value = pmt_value;

}

function conv_number(expr, decplaces) {

	// This function is from David Goodman's Javascript Bible.	

	var str = "" + Math.round(eval(expr) * Math.pow(10,decplaces));

	while (str.length <= decplaces) {

		str = "0" + str;

	}

	var decpoint = str.length - decplaces;

	return (str.substring(0,decpoint) + "." + str.substring(decpoint,str.length));

}

function reset() {

	document.getElementById("amount").value = "";

	document.getElementById("rate").selectedIndex = 0;

	document.getElementById("term").selectedIndex = 4;

	document.getElementById("balloon").value = "0.00";

	document.getElementById("balloon2").value = "0.00";

	document.getElementById("monthly").value = "";

}

function update_balloon_type(elem) {

	update_balloon_values();

	validBalloon = elem.id;

}

function update_balloon_values() {

	// we update the opposite balloon figure from the 'valid' one

	if ( ! IsNumeric ( document.getElementById('amount') ) ) {

		return;

	}

	if ( validBalloon == 'balloon' ) {

		if ( ! IsNumeric(document.getElementById("balloon").value) ) {

			return;

		}

		value = (document.getElementById("balloon").value * 100 / document.getElementById('amount').value).toFixed(2);

		if ( value > 100 ) {

			alert("Balloon payment larger than total amount to finance, please lower the balloon amount");

			return;

		}

		document.getElementById('balloon2').value = value;

	} else {

		if ( ! IsNumeric(document.getElementById("balloon2").value) ) {

			return;

		}

		if ( document.getElementById("balloon2").value > 100 ) {

			alert("Balloon payment larger than total amount to finance, please lower the balloon amount");

			return;

		}

		value = (document.getElementById("balloon2").value * document.getElementById('amount').value / 100).toFixed(2);

		document.getElementById('balloon').value = value;

	}

}

function update_values() {

	update_balloon_values();

	if ( document.getElementById('amount').value != "" ) {

		pmt();

	}

}


// ---- disappearing/reappearing text in the signup and search text field ---------------------------------------


function focusName(element) {		if (element.value == " name...")	{ element.value = ""; }}
function blurName(element) {		if (element.value == "")			{ element.value = " name..."; } }

function focusFirstName(element) {		if (element.value == " first name...")	{ element.value = ""; }}
function blurFirstName(element) {		if (element.value == "")			{ element.value = " first name..."; } }

function focusYourName(element) {	if (element.value == " your name")	{ element.value = ""; } }
function blurYourName(element) { 	if (element.value == "")			{ element.value = " your name"; } }

function focusEmail(element) {		if (element.value == " email...")	{ element.value = ""; } }
function blurEmail(element) {		if (element.value == "")			{ element.value = " email..."; } }

function focusSurname(element) {		if (element.value == " surname...")	{ element.value = ""; } }
function blurSurname(element) {		if (element.value == "")			{ element.value = " surname..."; } }

function focusConfirmEmail(element) {		if (element.value == " confirm email...")	{ element.value = ""; } }
function blurConfirmEmail(element) {		if (element.value == "")			{ element.value = " confirm email..."; } }

function focusYourEmail(element) {	if (element.value == " your email")	{ element.value = ""; } }
function blurYourEmail(element) {	if (element.value == "")			{ element.value = " your email"; } }

function focusKeyword(element) {	if (element.value == " keyword...")	{ element.value = ""; } }
function blurKeyword(element) {		if (element.value == "")			{ element.value = " keyword..."; } }

function focusUsername(element) {	if (element.value == "username")	{ element.value = ""; } }
function blurUsername(element) {	if (element.value == "")			{ element.value = "username"; } }

function focusPassword(element) {	if (element.value == "password")	{ element.value = ""; } }
function blurPassword(element) {	if (element.value == "")			{ element.value = "password"; } }

function focusThis(element, text){ if (element.value == text)	{ element.value = ""; } }
function blurThis(element, text){ if (element.value == "")	{ element.value = text; }}
// ---- disappearing/reappearing text in the signup and search text field ---------------------------------------



function submit_calculator() {


	if ( document.getElementById('conmsg_name').value == "" ) {

		alert("Your name please");

		document.getElementById('conmsg_name').focus();

		return false;

	}
	

	if ( document.getElementById('conmsg_phone').value != "" ) {
	
		var length = document.getElementById('conmsg_phone').value.length ;
		var phone = document.getElementById('conmsg_phone').value ;
		var validChars = "0123456789 ()" ;
		
		// tests to insure it is a valid phone number with no letters, "() -" are  accepted 
		for (var i=0; i<length; i++) { 
      		Char = phone.charAt(i); 
      		if (validChars.indexOf(Char) == -1) 
         	{

			alert('you have typed '+(document.getElementById('conmsg_phone').value)+'\n" '+Char+' " is not an accepted input');
			
			document.getElementById('conmsg_phone').focus();
			
			return false;
         	}
      	}
		
		// validates length of phone number
	  	
		for(var x = 1; x<length; ++x){
	   	phone = phone.replace(' ','');   
	 	}
		
		for(var x = 1; x<length; ++x){
	   	phone = phone.replace('(','');   
	 	}
		
		for(var x = 1; x<length; ++x){
	  	phone = phone.replace(')','');   
	 	}
		
		for(var x = 1; x<length; ++x){
	   	phone = phone.replace('-','');   
	 	}
		if (phone.length < 9){
		
		alert('you have typed '+(document.getElementById('conmsg_phone').value)+'\nit appears to be incomplete?');
			
		document.getElementById('conmsg_phone').focus();
			
		return false;
		
		}
	
	}else{

		alert("Your phone number please");
	
		document.getElementById('conmsg_phone').focus();
			
		return false;
	}
	
	
	
	
	if (document.getElementById('conmsg_email').value != "") {
	
		var length = document.getElementById('conmsg_email').value.length ;
		var dotIndex = document.getElementById('conmsg_email').value.lastIndexOf(".");
		var atIndex = document.getElementById('conmsg_email').value.indexOf("@");
		var emailT = document.getElementById('conmsg_email').value.slice(atIndex);
		var eTAtIndex = emailT.indexOf("@") ;
		var eTDotIndex = emailT.indexOf(".") ;		
		
		if (length-dotIndex < 3 || atIndex < 1 || dotIndex < 1 || eTDotIndex-eTAtIndex < 2){
		
		alert('We need a Valid formatted Email address please' );
		
		document.getElementById('conmsg_email').focus();

		return false;
		}

	}else{

		alert("your email please");
	
		document.getElementById('conmsg_email').focus();
			
		return false;
	}
	
	
	if ( document.getElementById('conmsg_amount').value == "" || (! IsNumeric(document.getElementById('conmsg_amount').value)) ) {

		alert("The value of the amount financed (price) must be a valid number");

		document.getElementById('conmsg_amount').focus();

		return false;

	}
	
    if ( document.getElementById('conmsg_postcode').value == "" ) {

		alert("Your postcode please");

		document.getElementById('conmsg_postcode').focus();

		return false;

	}
	
   /* if ( document.getElementById('balloon').value == "" || IsNumeric(document.getElementById('balloon').value) == false ) {

		alert("The value of the final payment (balloon) must be a valid number");

		document.getElementById('balloon').focus();

		return false;

	}
	
		if ( document.getElementById('balloon2').value == "" || IsNumeric(document.getElementById('balloon2').value) == false ) {

		alert("The value of the final payment (percentage) must be a valid number");

		document.getElementById('balloon2').focus();

		return false;

	}
	
	update_values(); */

    $.fn.popupWindow ({ 
        windowURL:'/terms-and-conditions.php', 
        windowName:'Terms And Conditions' ,
        height:500, 
        width:720, 
        centerBrowser:1 
    });
    return false;
	///document.forms.calculator.submit();

}
